<template>
	<view class="identity-main text-center">
		<view class="tips-box checked">
			<text class="theme">{{ state == '1' ? '应聘者' : '招聘者' }}</text>
		</view>
		<image :class="{'img-box': true, 'applicant': state == '1','recruit': state !== '1'}" :src="state == '1' ? 'https://fdfs.unifgroup.com:8888/group1/M00/02/60/ClEhn16O7ROAaOqfAAIMiFclEkA068.png' : 'https://fdfs.unifgroup.com:8888/group1/M00/02/60/ClEhn16O7ROANAeaAARhodJlpsE067.png'">
		</image>
		<view class="btn-box">
			<button class="theme-submit-btn" @click="checkIdentityFn">切换为{{ state == '1' ? '招聘者' : '应聘者' }}身份</button>
			<button class="theme-back-btn" @click="navigateBackFn">返回</button>
		</view>
	</view>
</template>

<script>
	import {
		mapGetters
	} from "vuex"
	import {
		setChangeInfo
	} from '../../../apis/recruit/index.js'

	export default {
		data() {
			return {
				state: '1', //2 招聘者  1 应聘者
			}
		},
		onLoad(options) {
			this.state = uni.getStorageSync('identity')
		},
		computed: {
			isBoss() {
				return uni.getStorageSync('identity') == 2
			},
			...mapGetters(["myTimInfo"])
		},
		methods: {
			navigateBackFn() {
				uni.navigateBack({
					delta: 1,
				})
			},
			timLogin() {
				const identity = this.isBoss ? 'gdx' : '',
					userID = uni.getStorageSync("openId") + identity,
					userSig = this.isBoss ? uni.getStorageSync("userSigBoss") : uni.getStorageSync("userSig");
					
				this.login(userID, userSig)
			},
			login(userID, userSig) {

				uni.$Tim.login({
					userID,
					userSig
				}).then(() => {
					console.log('tim登录成功!')
				}).catch(() => {
					console.log('tim登录失败!!!')
				})
			},
			checkIdentityFn() {
				let _this = this;
				let params = {
					identity: this.state == '1' ? '2' : '1'
				}

				setChangeInfo(params).then(res => {
					if (res.code == 1) {
						_this.state = params.identity

						uni.setStorageSync('identity', +_this.state)
						_this.$store.dispatch('user/setIdentity', +_this.state)
						res.data.im_token_boss && uni.setStorageSync('userSigBoss', res.data.im_token_boss)
						res.data.im_token && uni.setStorageSync('userSig', res.data.im_token)


						if (uni.$Tim) {
							_this.timLogin()
							uni.navigateBack({
								delta: 1,
							})

							// uni.$Tim.logout().then(imResponse => {
							// 		_this.timLogin()
							// 		uni.navigateBack({
							// 			delta: 1,
							// 		})
							// 	})
							// 	.catch(err => {
							// 		console.warn('logout error:', err);
							// 	});
						} else {
							uni.navigateBack({
								delta: 1,
							})
						}
					}
				})
			},
		}
	}
</script>

<style lang="less">
	.identity-main {
		.tips-box {
			margin-top: 75rpx;
			font-size: 83rpx;

			&.recruit {
				max-width: 644rpx;
				height: 40vh;
				margin: 100rpx 0 55rpx;
			}
		}

		.img-box.applicant {
			max-width: 688rpx;
			max-height: 40vh;
			margin: 90rpx auto 0;
		}

		.btn-box {
			margin-top: 170rpx;

			.theme-back-btn {
				margin-top: 44rpx;
			}
		}

		.btn-back {
			margin-top: 44rpx;
		}
	}
</style>
